import java.io.*;
import java.util.Random;
import java.util.Scanner;

public class Administrator {
    String id;
    String pw;
    public void enter(){
        File read = new File("src/admin-data");
        try {
            Reader in = new FileReader(read);
            BufferedReader bufferedReader = new BufferedReader(in);
            String str = bufferedReader.readLine();
            String[] account = str.split(" ");
            bufferedReader.close();
            in.close();
            System.out.println("请输入账号");
            Scanner scanner = new Scanner(System.in);
            id = scanner.next();
            System.out.println("请输入密码");
            pw = scanner.next();
            if(account[0].equals(id) &&account[1].equals(pw)){
                System.out.println("登录成功，欢迎您，管理员");
                menu();
            }
            else {
                enter();
            }
        }catch (IOException e){
            System.out.println(e.toString());
        }
    }
    public void menu(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("~~~~~购物管理系统~~~~~");
        System.out.println("1.密码管理");
        System.out.println("2.用户管理");
        System.out.println("3.商品管理");
        System.out.println("4.登出系统");
        int choice = scanner.nextInt();
        if(choice < 1 || choice > 4){
            System.out.println("请输入1~4的整数");
            choice = scanner.nextInt();
        }
        switch (choice){
            case 1:
                passwordManage();
                break;
            case 2:
                customerManage();
                break;
            case 3:
                goodsManage();
                break;
            case 4:
                System.out.println("退出成功！");
                break;
        }
    }
    public void passwordManage(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("~~~~~密码管理系统~~~~~");
        System.out.println("1.密码修改");
        System.out.println("2.用户密码重置");
        System.out.println("3.返回上一级");
        int choice = scanner.nextInt();
        if(choice < 1 || choice > 3){
            System.out.println("请输入1~3的整数");
            passwordManage();
        }
        switch (choice){
            case 1:
                passwordChange();
                break;
            case 2:
                System.out.println("请输入要重置的用户id");
                String id = scanner.next();
                passwordReset();
                break;
            case 3:
                menu();
                break;
        }
    }
            public void passwordChange(){
                System.out.println("请输入原密码");
                Scanner scanner = new Scanner(System.in);
                String str = scanner.next();
                if(this.pw.equals(str)){
                    System.out.println("请输入新密码");
                    str = scanner.next();
                    File file = new File("src/admin-data");
                    File temp = new File("src/temp");
                    try {
                        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(temp));
                        String string = bufferedReader.readLine();
                        while(string != null){
                            String[] account = string.split(" ");
                            if(account[0].equals(id)){
                                account[1]=str;
                            }
                            String x = "";
                            for(String y:account){
                                x += y+" ";
                            }
                            bufferedWriter.write(x);
                            string = bufferedReader.readLine();
                        }
                        bufferedReader.close();
                        bufferedWriter.close();
                        new File("src/admin-data").delete();
                        new File("src/temp").renameTo(file);
                        this.pw = str;
                        System.out.println("修改成功，请重新登录");
                        enter();
                    }catch (IOException e){
                        System.out.println(e.toString());
                    }
                }else {
                    System.out.println("密码错误");
                    passwordChange();
                }

            }
            public void passwordReset(){
            }
                public static String randomPassword() {
                    Scanner scanner = new Scanner(System.in);
                    System.out.println("请输入要重置的用户id");
                    String id = scanner.next();
                    String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
                    Random random = new Random();
                    StringBuilder password = new StringBuilder();
                    for (int i = 0; i < 6; i++) {
                        int index = random.nextInt (characters.length ());
                      password.append (characters.charAt (index));
                    }
                    return password.toString ();
                }
    public void customerManage(){
        System.out.println("~~~~~客户管理系统~~~~~");
        System.out.println("1.列出所有用户信息");
        System.out.println("2.用户删除");
        System.out.println("3.查询用户信息");
        System.out.println("4.返回上一级");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        if(choice < 1 || choice > 4){
            System.out.println("请输入1~4的整数");
            customerManage();
        }
        switch (choice){
            case 1:
                customerDisplay();
                break;
            case 2:
                customerDelete();
                break;
            case 3:
                customerRefer();
                break;
            case 4:
                menu();
                break;
        }
    }
            public void customerDisplay(){
                File file = new File("src/Customer-data");
                try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
                    String str;
                    str = bufferedReader.readLine();
                    while (str != null) {
                        String[] data = str.split(" ");
                        System.out.println("手机号/用户id:" + data[0] + " 用户名：" + data[2] + " 级别：" + data[3] + " 注册时间：" + data[4] + " 累计消费金额：" + data[5] + " 用户邮箱：" + data[6]);
                        str = bufferedReader.readLine();
                    }
                    Scanner scanner = new Scanner(System.in);
                    System.out.println("返回上一级，请输入0");
                    int choice = scanner.nextInt();
                    if (choice == 0) {
                        customerManage();
                    }
                } catch (IOException e) {
                    System.out.println(e.toString());
                }
            }
            public void customerDelete(){
                System.out.println("请输入要删除的用户id");
                Scanner scanner = new Scanner(System.in);
                String s = scanner.next();
                System.out.println("确定要删除吗？（1.确定 2.返回上一级）");
                int choice = scanner.nextInt();
                if(choice == 2){
                    customerManage();
                }
                if(choice == 1){
                    File file = new File("src/Customer-data");
                    File temp = new File("src/temp");
                    try {
                        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(temp,true));
                        String string = bufferedReader.readLine();
                        while(string != null){
                            String[] account = string.split(" ");
                            if(account[0].equals(s)){
                                string = bufferedReader.readLine();
                                continue;
                            }
                            String x = "";
                            for(String y:account){
                                x += y+" ";
                            }
                            bufferedWriter.write(x+"\n");
                            string = bufferedReader.readLine();
                        }
                        bufferedReader.close();
                        bufferedWriter.close();
                        new File("src/Customer-data").delete();
                        new File("src/temp").renameTo(file);
                        System.out.println("删除成功，返回上一级");
                        customerManage();
                    }catch (IOException e){
                        System.out.println(e.toString());
                    }
                }
            }
            public void customerRefer(){
                System.out.println("1.查询单个用户");
                System.out.println("2.查询所有用户");
                Scanner scanner = new Scanner(System.in);
                int choice = scanner.nextInt();
                if(choice < 1 || choice > 2){
                    System.out.println("请输入1~2的整数");
                    customerRefer();
                }
                switch (choice){
                    case 1:
                        customerSingleRefer();
                        break;
                    case 2:
                        customerDisplay();
                        break;
                }
            }
                public void customerSingleRefer(){
                    System.out.println("请输入查询用户的id");
                    Scanner scanner = new Scanner(System.in);
                    String s = scanner.next();
                    File file = new File("src/Customer-data");
                    try {
                        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                        String string = bufferedReader.readLine();
                        while(string != null){
                            String[] data = string.split(" ");
                            if(data[0].equals(s)){
                                System.out.println("手机号/用户id:"+data[0]+" 用户名:"+data[2]+" 级别:"+data[3]+" 注册时间:"+data[4]+" 累计消费金额:"+data[5]+" 用户邮箱:"+data[6]);
                            }
                            string = bufferedReader.readLine();
                        }
                        bufferedReader.close();
                        System.out.println("返回上一级,请输入0");
                        int choice = scanner.nextInt();
                        if(choice == 0 ){customerManage();}
                    }catch (IOException e){
                        System.out.println(e.toString());
                    }
                }

    public void goodsManage(){
        System.out.println("商品管理系统");
        Scanner scanner = new Scanner(System.in);
        System.out.println("1.列出所有商品信息");
        System.out.println("2.添加商品");
        System.out.println("3.修改商品信息");
        System.out.println("4.删除商品");
        System.out.println("5.返回上一级");
        int choice = scanner.nextInt();
        if(choice < 1 || choice > 5){
            System.out.println("请输入1~5的整数");
            goodsManage();
        }
        switch (choice){
            case 1:
                goodsDisplay();
                break;
            case 2:
                goodsAdd();
                break;
            case 3:
                goodsChange();
                break;
            case 4:
                goodsDelete();
                break;
            case 5:
                menu();
                break;
        }
    }
        public void goodsDisplay(){
            File file = new File("src/goods-data");
            try{
                BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                String str = bufferedReader.readLine();
                while (str != null){
                    String[] data = str.split(" ");
                    System.out.println("商品id:"+data[0]+" 商品名:"+data[1]+" 生产厂家:"+data[2]+" 生产日期:"+data[3]+" 进货价:"+data[4]+" 零售价:"+data[5]+" 数量:"+data[6]);
                    str = bufferedReader.readLine();
                }
                bufferedReader.close();
                Scanner scanner = new Scanner(System.in);
                System.out.println("返回上一级，请输入0");
                int choice = scanner.nextInt();
                if(choice == 0){
                    goodsManage();
                }
            }catch (IOException e){
                System.out.println(e.toString());
            }
        }
        public void goodsAdd(){
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要添加的商品信息");
            System.out.println("商品id：");
            String a = scanner.next();
            System.out.println("商品名：");
            String b = scanner.next();
            System.out.println("生产厂家：");
            String c = scanner.next();
            System.out.println("生产日期：");
            String d = scanner.next();
            System.out.println("进货价：");
            String e = scanner.next();
            System.out.println("零售价：");
            String f = scanner.next();
            System.out.println("数量：");
            String g = scanner.next();
            File file = new File("src/goods-data");
            try {
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file,true));
                bufferedWriter.write(a+" "+b+" "+c+" "+d+" "+e+" "+f+" "+g+"\n");
                bufferedWriter.close();
            }catch (IOException ex){
                System.out.println(ex.toString());
            }
            System.out.println("商品添加成功！");
            goodsManage();
        }
        public void goodsChange(){
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要修改的商品id:");
            String s = scanner.next();
            File file = new File("src/goods-data");
            try {
                BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                String string = bufferedReader.readLine();
                while(string != null){
                    String[] data = string.split(" ");
                    if(data[0].equals(s)){
                        System.out.println("1.商品id:"+data[0]+" 2.商品名:"+data[1]+" 3.生产厂家:"+data[2]+" 4.生产日期:"+data[3]+" 5.进货价:"+data[4]+" 6.零售价:"+data[5]+" 7.数量:"+data[6]);
                    }
                    string = bufferedReader.readLine();
                }
                bufferedReader.close();
            }catch (IOException e){
                System.out.println(e.toString());
            }
            System.out.println("请选择要修改的部分:");
            int num = scanner.nextInt();
            System.out.println("请输入修改后的内容：");
            String ss = scanner.next();
            file = new File("src/goods-data");
            File temp = new File("src/temp");
            try {
                BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(temp,true));
                String string = bufferedReader.readLine();
                while(string != null){
                    String[] account = string.split(" ");
                    if(account[0].equals(s)){
                        account[num-1] = ss ;
                    }
                    String x = "";
                    for(String y:account){
                        x += y+" ";
                    }
                    bufferedWriter.write(x+"\n");
                    string = bufferedReader.readLine();
                }
                bufferedReader.close();
                bufferedWriter.close();
                new File("src/goods-data").delete();
                new File("src/temp").renameTo(file);
            }catch (IOException e){
                System.out.println(e.toString());
            }
            System.out.println("修改完成，即将返回上一级");
            goodsManage();
        }
        public void goodsDelete(){
            System.out.println("请输入要删除的商品id");
            Scanner scanner = new Scanner(System.in);
            String s = scanner.next();
            System.out.println("确定要删除吗？（1.确定 2.返回上一级）");
            int choice = scanner.nextInt();
            if(choice == 2){
                goodsManage();
            }else if(choice == 1){
                File file = new File("src/goods-data");
                File temp = new File("src/temp");
                try {
                    BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(temp,true));
                    String string = bufferedReader.readLine();
                    while(string != null){
                        String[] account = string.split(" ");
                        if(account[0].equals(s)){
                            string = bufferedReader.readLine();
                            continue;
                        }
                        String x = "";
                        for(String y:account){
                            x += y+" ";
                        }
                        bufferedWriter.write(x+"\n");
                        string = bufferedReader.readLine();
                    }
                    bufferedReader.close();
                    bufferedWriter.close();
                    new File("src/goods-data").delete();
                    new File("src/temp").renameTo(file);
                    System.out.println("删除成功，返回上一级");
                    goodsManage();
                }catch (IOException e){
                    System.out.println(e.toString());
                }
            }
            else {
                System.out.println("请输入1或2");
                goodsDelete();
            }
        }
}
